/**
 * Created by Administrator on 2015/12/6.
 */
var mongodb = require('mongodb');
var ObjectID = mongodb.ObjectID;
var MongoClient = mongodb.MongoClient;
var assert = require('assert');
var Q = require('q');
var url = 'mongodb://localhost:27017/mydb';
var conn = require('./conn');
/**
 * {_id:
 *  username:
 *  nickname
 *  email:
 *  password:
 *  date://注册时间
 *  }
 */
function getUser(_id,callback){
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var blog = db.collection('user');
        connection = db;
        return blog.findOne({_id:ObjectID.createFromHexString(_id)});
    }).then(function(doc){
        doc._id = doc._id.toHexString();
        //delete doc.password;//删除密码属性
        callback(doc);
    }).finally(function(){
        connection.close();
    })
}

function login(username,password,callback){
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var user = db.collection('user');
        connection = db;
        return user.findOne({username:username,password:password});
    }).then(function(doc){//如果返回空对象就是{} login失败
        doc._id = doc._id.toHexString();

        callback(doc);
    }).finally(function(){
        connection.close();
    })
}

function register(userInfo,callback){//应该回显_id
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var user = db.collection('user');
        connection = db;
        return user.insertOne(userInfo);
    }).then(function(doc){//如果返回与插入有关的信息包括insertId
        callback(doc);
    }).finally(function(){
        connection.close();
    })
}

var myuser = {
    username:'dongge',
    nickname:'百度',
    email:'442704997@qq.com',
    password:'123456',
    date:Date.now()
};
register(myuser,console.log);

function edit(_id,editInfo,callback){
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var user = db.collection('user');
        connection = db;
        return user.updateOne({_id:ObjectID(_id)},{$set:editInfo});
    }).then(function(doc){//如果返回与插入有关的信息包括insertId
        callback(doc);
    }).finally(function(){
        connection.close();
    })
}

function remove(_id,callback){
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var user = db.collection('user');
        connection = db;
        return user.removeOne({_id:ObjectID(_id)});
    }).then(function(doc){//删除信息
        callback(doc.deletedCount);
    }).finally(function(){
        connection.close();
    })
}

function getUserList(page,callback){
    var p = conn.getConnection();
    var connection = undefined;
    p.then(function(db){
        var user = db.collection('user');
        connection = db;
        return [user.find({}).skip((page-1)*5).limit(5).toArray(),user.find({}).count()];
    },function(err){
        console.log(err);
    }).spread(function(docs,count){
        docs.forEach(function(doc){
            doc._id = doc._id.toHexString();
        });
        callback({
            total:count,
            users:docs
        })
    }).finally(function(){
        connection.close();
    })
}

//getUserList(1,console.log);
//remove('5678d562acec69c6aa8c1631',console.log);
//edit('56790017a978f8800eb0310a',{username:'百年孤独'},console.log);